package com.woniu.his.pms.service.impl;



import ch.qos.logback.core.joran.action.ParamAction;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.exception.HisException;
import com.woniu.his.pms.common.PmsResultCode;
import com.woniu.his.pms.infra.mapper.PatientMapper;
import com.woniu.his.pms.infra.model.Patient;
import com.woniu.his.pms.service.PatientService;
import com.woniu.his.pms.service.dto.PatientDto;
import com.woniu.his.pms.service.dto.PatientLoginDto;
import com.woniu.his.pms.service.dto.StartDiagnosePatientDto;
import com.woniu.his.pms.service.param.AddPatientServiceParam;
import com.woniu.his.pms.service.param.PhoneLoginServiceParam;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 患者信息表 服务实现类
 * </p>
 *
 * @author ZJH
 * @since 2022-11-25
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Resource
    private Snowflake snowflake;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PatientDto addPatient(AddPatientServiceParam param) {
        //从数据库查询患者更新信息
        Patient patient = getById(param.getId());
        if(patient==null) throw new HisException(PmsResultCode.PATIENT_NOT_EXISTS);
        patient.setDataOfBirth(param.getDataOfBirth());
        patient.setGender(param.getGender());
        patient.setHomeAddress(param.getHomeAddress());
        patient.setIdentificationNo(param.getIdentificationNo());
        patient.setName(param.getName());
        patient.setMedicalRecordNo("patient"+snowflake.nextId());
        updateById(patient);
        return BeanUtil.copyProperties(patient,PatientDto.class);
    }

    @Override
    public List<PatientDto> queryPatient(List<Long> patientIds) {
        List<Patient> patients = list(new QueryWrapper<Patient>().in("id", patientIds));
        return BeanUtil.copyToList(patients,PatientDto.class);
    }

    @Override
    public PatientDto queryPatientByIdentity(String identity) {
        Patient patient = getOne(new QueryWrapper<Patient>().eq("identificationNo", identity));
        return BeanUtil.copyProperties(patient,PatientDto.class);
    }

    @Override
    public StartDiagnosePatientDto queryPatientById(Long patientId) {
        Patient patient = getById(patientId);
        StartDiagnosePatientDto startDiagnosePatientDto = new StartDiagnosePatientDto();
        startDiagnosePatientDto.setName(patient.getName());
        startDiagnosePatientDto.setGender(patient.getGender());
        int age = DateUtil.ageOfNow(patient.getDataOfBirth());
        startDiagnosePatientDto.setAge(age+"岁");
        return startDiagnosePatientDto;
    }

    @Override
    public PatientLoginDto patientPhoneLogin(PhoneLoginServiceParam phoneLoginServiceParam) {
        String redisCode=stringRedisTemplate.opsForValue().get("patient:login:code:"+phoneLoginServiceParam.getUserPhone());
        //判断验证码是否一致
        if (redisCode==null||redisCode.trim().equals("")) throw new HisException(PmsResultCode.CODE_NOT_EXIST_OR_EXPIRED);
        if(!redisCode.equals(phoneLoginServiceParam.getCode())) throw new HisException(PmsResultCode.CODE_NOT_TRUE);
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("phone_no", phoneLoginServiceParam.getUserPhone());
        Patient one = getOne(wrapper);
        if (one == null) {
            one = new Patient();
            one.setPhoneNo(phoneLoginServiceParam.getUserPhone());
            save(one);
        }
        PatientLoginDto result=BeanUtil.copyProperties(one,PatientLoginDto.class);
        return result;
    }
}
